var api = require('../../config/api.js');
var app = getApp()

Page({
  data: {
    id:'',
    expense:'',
    activeIndex:''


  },

  expenseInput: function (e) {
    this.setData({
      expense: e.detail.value
    })
  },

  //修改商家运费
  updateSubmit: function (e) {
    var that = this;
    wx.showModal({
      title: '提示',
      content: '是否确认提交',
      confirmColor: "#43CBC9",
      success: (res) => {
        wx.showLoading({
          title: '加载中',
        })

        if (res.confirm) {
          wx.request({
            url: api.TransportationExpensesUpdate,
            data: {
              // id: wx.getStorageSync('incentiveId'),
              // incentivesContent:that.data.incentivesContent
              id:that.data.id,
              expense: that.data.expense
            },
            method: 'POST',
            header: {
              'Accept': 'application/json',
              'ACCESS-TOKEN': wx.getStorageSync('token')
            },
            success: function (res) {
              // wx.setStorageSync('incentiveId', res.data.obj.id);
              // wx.setStorageSync('incentivesContent', res.data.obj.incentivesContent);
              wx.hideLoading();
              if (res.data.success) {
                wx.showModal({
                  title: '提示',
                  content: "商家运费修改成功！",
                  confirmColor: "#43CBC9",
                  showCancel: false,
                  success: function (res) {
                    if (res.confirm) {
                      console.log('用户点击确定')
                      wx.navigateBack({})
                    }
                  }
                });
                wx.hideLoading();
              } else {
                console.log("数据更新不成功！")
              }
            },
            fail: function () {
              wx.hideLoading();
              wx.showToast({
                title: res.data.msg,
                icon: 'none',
                duration: 2000
              });
            },
          })

        } else if (res.cancel) {
          console.log('用户点击取消')
          wx.hideLoading();
        }
      }
    })
  },

  //提交商家运费
  formSubmit: function (e) {
    var that = this;
    wx.showModal({
      title: '提示',
      content: '是否确认提交',
      confirmColor: "#43CBC9",
      success: (res) => {
        wx.showLoading({
          title: '加载中',
        })

        if (res.confirm) {
          console.log('for发生了submit事件，携带数据为：', e.detail.value);
          if (e.detail.value.expense == '') {
            wx.showToast({
              title: "请填写商家运费!",
              icon: 'none',
              duration: 2000
            });
            return false;
          } else if (!/^\d+(\.\d{1,2})?$/.test(e.detail.value.expense)) {
            wx.showToast({
              title: "商家运费必须是两位小数的价格!",
              icon: 'none',
              duration: 2000
            });
            return false;
          } 

          wx.request({
            url: api.TransportationExpensesAdd,
            data: {
              // incentivesContent: e.detail.value.incentivesContent
              expense: that.data.expense

            },
            method: 'POST',
            header: {
              'Accept': 'application/json',
              'ACCESS-TOKEN': wx.getStorageSync('token')
            },
            success: function (res) {
              wx.hideLoading();
              if (res.data.success) {
                // wx.setStorageSync('incentiveId', res.data.obj.id);
                // wx.setStorageSync('incentivesContent', res.data.obj.incentivesContent);
                wx.showModal({
                  title: '提示',
                  content: "添加商家运费成功!",
                  confirmColor: "#43CBC9",
                  showCancel: false,
                  success: function (res) {
                    if (res.confirm) {                 
                      console.log('用户点击确定')
                      wx.navigateBack({});
                      that.onLoad()
                    }
                  }
                });
                wx.hideLoading();
              } else {
                wx.showToast({
                  title: res.data.msg,
                  icon: 'none',
                  duration: 2000
                });
              }
            },
            fail: function () {
              wx.hideLoading();
              wx.showToast({
                title: res.data.msg,
                icon: 'none',
                duration: 2000
              });
            },
          })

        } else if (res.cancel) {
          console.log('用户点击取消')
          wx.hideLoading();
        }
      }
    })
  },

  //取消商家运费的信息修改
  canback: function () {
    wx.navigateBack({})
  },

  onLoad: function(options) {
    // 页面初始化 options为页面跳转所带来的参数
    var that = this;
    that.setData({
      id: '',
      expense: ''
    });


  },
  onReady: function() {
    var that=this;
    wx.request({
      url: api.TransportationExpensesInfoByBusinessId + wx.getStorageSync('deptId'),
      data: {
        
      },
      method: 'GET',
      header: {
        'Accept': 'application/json',
        'ACCESS-TOKEN': wx.getStorageSync('token')
      },
      success: function (res) {
        if (res.data.success) {
          that.setData({
            activeIndex: '0',
            id: res.data.obj.id,
            expense: res.data.obj.expense
          });
          // wx.setStorageSync('incentiveId', res.data.obj.id);
          // wx.setStorageSync('incentivesContent', res.data.obj.incentivesContent);
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 2000
          });
          console.log(res.data.msg);
        }
      },
      fail: function () {
        wx.hideLoading();
        wx.showToast({
          title: res.data.msg,
          icon: 'none',
          duration: 2000
        });
      },
    })
  },
  onShow: function() {
    // 页面显示
  },
  onHide: function() {
    // 页面隐藏
  },
  onUnload: function() {
    // 页面关闭
  }
})


// Page({
//   data: {

//     checkboxItems: [
//       { name: '按摩颈部', value: '0', checked: true },
//       { name: '按摩肩部', value: '1' },
//       { name: '按摩背部', value: '2' },
//       { name: '按摩腰部', value: '3' },
//       { name: '按摩手臂', value: '4' },
//       { name: '按摩腿部', value: '5' },
//       { name: '按摩脚部', value: '6' }
//     ],
//   },

//   checkboxChange: function (e) {
//     console.log('checkbox发生change事件，携带value值为：', e.detail.value);

//     var checkboxItems = this.data.checkboxItems, values = e.detail.value;
//     for (var i = 0, lenI = checkboxItems.length; i < lenI; ++i) {
//       checkboxItems[i].checked = false;

//       for (var j = 0, lenJ = values.length; j < lenJ; ++j) {
//         if (checkboxItems[i].value == values[j]) {
//           checkboxItems[i].checked = true;
//           break;
//         }
//       }
//     }

//     this.setData({
//       checkboxItems: checkboxItems
//     });
//   },

//   onLoad: function (options) {
//     // 页面初始化 options为页面跳转所带来的参数


//   },
//   onReady: function () {
//     // 页面渲染完成
//   },
//   onShow: function () {
//     // 页面显示
//   },
//   onHide: function () {
//     // 页面隐藏
//   },
//   onUnload: function () {
//     // 页面关闭
//   }
// })